package com.gb.demand.util;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONException;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.gb.demand.common.dto.SkuArray;
import com.gb.demand.common.dto.SkuInfo;
import com.gb.demand.common.dto.SkuSalesReturn;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigDecimal;

/**
 * 新点第三方接口
 */
@Slf4j
public class XindianUtil {

    public static final String IP = "47.110.240.241";

    public static final String APPKEY = "epoint";

    public static final String APPSECRET = "7db8fbcc-a9c2-4010-bc79-e7c4bcd9ab04";

    public static final String ECNAME = "";

    public static final String PLATFORMCODE = "";

    /**
     * 调用获取接口令牌接口
     *
     * @return
     */
    public synchronized static String getAccessToken(HttpServletRequest requests) {
        JSONObject body = new JSONObject();
        JSONObject requestData = new JSONObject();
        try {
            body.put("appKey", APPKEY);
            body.put("appSecret", APPSECRET);
            requestData.put("data", body);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8"), String.valueOf(requestData));
        OkHttpClient okHttpClient = new OkHttpClient();
        //创建一个请求对象
        Request request = new Request.Builder()
                .url("http://" + IP + "/ecservice/getAccessToken")
                .post(requestBody)
                .build();
        //发送请求获取响应
        Response response = null;
        try {
            response = okHttpClient.newCall(request).execute();
            //判断请求是否成功
            if (response.isSuccessful()) {
                //判空
                if (response.body() != null) {
                    //打印服务端返回结果
                    log.info(response.body().string());
                    JSONObject jsonObject = JSONUtil.parseObj(response.body().string());
                    String isSuccess = (String) jsonObject.get("isSuccess");
                    if ("true".equals(isSuccess)) {
                        String accessToken = String.valueOf(jsonObject.get("accessToken"));
                        //将token存到session中
                        HttpSession session = requests.getSession();
                        session.setAttribute("accessToken", accessToken);
                        //设置过期时间为24小时
                        session.setMaxInactiveInterval(60 * 60 * 24);
                        return accessToken;
                    }

                }
            } else {
                return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 调用获取品类(目录)接口
     *
     * @return
     */
    public synchronized static JSONObject getProductCategory(HttpServletRequest requests) {
        //在session中获取accessToken
        String accessToken = (String) requests.getAttribute("accessToken");
        if (ObjectUtil.isEmpty(accessToken)) {
            //如果过期了就再生成一个accessToken
            accessToken = getAccessToken(requests);
            //将token存到session中
            HttpSession session = requests.getSession();
            session.setAttribute("accessToken", accessToken);
            //设置过期时间为24小时
            session.setMaxInactiveInterval(60 * 60 * 24);
        }
        JSONObject body = new JSONObject();
        JSONObject requestData = new JSONObject();
        try {
            body.put("appKey", APPKEY);
            body.put("accessToken", accessToken);
            requestData.put("data", body);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8"), String.valueOf(requestData));
        OkHttpClient okHttpClient = new OkHttpClient();
        //创建一个请求对象
        Request request = new Request.Builder()
                .url("http://" + IP + "/ecservice/getProductCategory")
                .post(requestBody)
                .build();
        //发送请求获取响应
        Response response = null;
        try {
            response = okHttpClient.newCall(request).execute();
            //判断请求是否成功
            if (response.isSuccessful()) {
                //判空
                if (response.body() != null) {
                    //打印服务端返回结果
                    log.info(response.body().string());
                    JSONObject jsonObject = JSONUtil.parseObj(response.body().string());
                    String isSuccess = (String) jsonObject.get("isSuccess");
                    if ("true".equals(isSuccess)) {
                        JSONObject result = JSONUtil.parseObj(String.valueOf(jsonObject.get("result")));
                        return result;
                    }

                }
            } else {
                return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 调用获取商品池接口
     *
     * @return
     */
    public synchronized static JSONObject getProductPool(HttpServletRequest requests, String categoryId) {
        //在session中获取accessToken
        String accessToken = (String) requests.getAttribute("accessToken");
        if (ObjectUtil.isEmpty(accessToken)) {
            //如果过期了就再生成一个accessToken
            accessToken = getAccessToken(requests);
            //将token存到session中
            HttpSession session = requests.getSession();
            session.setAttribute("accessToken", accessToken);
            //设置过期时间为24小时
            session.setMaxInactiveInterval(60 * 60 * 24);
        }
        JSONObject body = new JSONObject();
        JSONObject requestData = new JSONObject();
        try {
            body.put("appKey", APPKEY);
            body.put("accessToken", accessToken);
            body.put("categoryId", categoryId);
            requestData.put("data", body);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8"), String.valueOf(requestData));
        OkHttpClient okHttpClient = new OkHttpClient();
        //创建一个请求对象
        Request request = new Request.Builder()
                .url("http://" + IP + "/ecservice/getProductPool")
                .post(requestBody)
                .build();
        //发送请求获取响应
        Response response = null;
        try {
            response = okHttpClient.newCall(request).execute();
            //判断请求是否成功
            if (response.isSuccessful()) {
                //判空
                if (response.body() != null) {
                    //打印服务端返回结果
                    log.info(response.body().string());
                    JSONObject jsonObject = JSONUtil.parseObj(response.body().string());
                    String isSuccess = (String) jsonObject.get("isSuccess");
                    if ("true".equals(isSuccess)) {
                        JSONObject sku = JSONUtil.parseObj(String.valueOf(jsonObject.get("sku")));
                        return sku;
                    }
                }
            } else {
                return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取商品详情接口
     *
     * @return
     */
    public synchronized static JSONObject getProductDetail(HttpServletRequest requests, String sku) {
        //在session中获取accessToken
        String accessToken = (String) requests.getAttribute("accessToken");
        if (ObjectUtil.isEmpty(accessToken)) {
            //如果过期了就再生成一个accessToken
            accessToken = getAccessToken(requests);
            //将token存到session中
            HttpSession session = requests.getSession();
            session.setAttribute("accessToken", accessToken);
            //设置过期时间为24小时
            session.setMaxInactiveInterval(60 * 60 * 24);
        }
        JSONObject body = new JSONObject();
        JSONObject requestData = new JSONObject();
        try {
            body.put("appKey", APPKEY);
            body.put("accessToken", accessToken);
            body.put("sku", sku);
            requestData.put("data", body);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8"), String.valueOf(requestData));
        OkHttpClient okHttpClient = new OkHttpClient();
        //创建一个请求对象
        Request request = new Request.Builder()
                .url("http://" + IP + "/getProductDetail")
                .post(requestBody)
                .build();
        //发送请求获取响应
        Response response = null;
        try {
            response = okHttpClient.newCall(request).execute();
            //判断请求是否成功
            if (response.isSuccessful()) {
                //判空
                if (response.body() != null) {
                    //打印服务端返回结果
                    log.info(response.body().string());
                    JSONObject jsonObject = JSONUtil.parseObj(response.body().string());
                    String isSuccess = (String) jsonObject.get("isSuccess");
                    if ("true".equals(isSuccess)) {
                        return jsonObject;
                    }
                }
            } else {
                return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 商品更新消息推送接口
     *
     * @return
     */
    public synchronized static JSONObject sendproductupdateinfo(String platformCode, String ecName, SkuInfo[] skuInfo) {
        JSONObject body = new JSONObject();
        JSONObject requestData = new JSONObject();
        try {
            body.put("platformCode", platformCode);
            body.put("ecName", ecName);
            body.put("skuInfo", skuInfo);
            requestData.put("data", body);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8"), String.valueOf(requestData));
        OkHttpClient okHttpClient = new OkHttpClient();
        //创建一个请求对象
        Request request = new Request.Builder()
                .url("http://" + IP + "/rest/product/sendproductupdateinfo")
                .post(requestBody)
                .build();
        //发送请求获取响应
        Response response = null;
        try {
            response = okHttpClient.newCall(request).execute();
            //判断请求是否成功
            if (response.isSuccessful()) {
                //判空
                if (response.body() != null) {
                    //打印服务端返回结果
                    log.info(response.body().string());
                    JSONObject jsonObject = JSONUtil.parseObj(response.body().string());
                    String isSuccess = (String) jsonObject.get("isSuccess");
                    if ("true".equals(isSuccess)) {
                        JSONObject returnMsg = JSONUtil.parseObj(String.valueOf(jsonObject.get("returnMsg")));
                        return returnMsg;
                    }
                }
            } else {
                return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 商品更新失败消息推送接口
     *
     * @return
     */
    public synchronized static JSONObject pushUpdateFailInfo(HttpServletRequest requests, String sku, Integer updateType, String failInfo) {
        //在session中获取accessToken
        String accessToken = (String) requests.getAttribute("accessToken");
        if (ObjectUtil.isEmpty(accessToken)) {
            //如果过期了就再生成一个accessToken
            accessToken = getAccessToken(requests);
            //将token存到session中
            HttpSession session = requests.getSession();
            session.setAttribute("accessToken", accessToken);
            //设置过期时间为24小时
            session.setMaxInactiveInterval(60 * 60 * 24);
        }
        JSONObject body = new JSONObject();
        JSONObject requestData = new JSONObject();
        try {
            body.put("appKey", APPKEY);
            body.put("accessToken", accessToken);
            body.put("sku", sku);
            body.put("updateType", updateType);
            body.put("failInfo", failInfo);
            requestData.put("data", body);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8"), String.valueOf(requestData));
        OkHttpClient okHttpClient = new OkHttpClient();
        //创建一个请求对象
        Request request = new Request.Builder()
                .url("http://" + IP + "/ecservice/pushUpdateFailInfo")
                .post(requestBody)
                .build();
        //发送请求获取响应
        Response response = null;
        try {
            response = okHttpClient.newCall(request).execute();
            //判断请求是否成功
            if (response.isSuccessful()) {
                //判空
                if (response.body() != null) {
                    //打印服务端返回结果
                    log.info(response.body().string());
                    JSONObject jsonObject = JSONUtil.parseObj(response.body().string());
                    String isSuccess = (String) jsonObject.get("isSuccess");
                    if ("true".equals(isSuccess)) {
                        JSONObject returnMsg = JSONUtil.parseObj(String.valueOf(jsonObject.get("returnMsg")));
                        return returnMsg;
                    }
                }
            } else {
                return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取商品评价接口
     *
     * @return
     */
    public synchronized static JSONObject getProductReview(HttpServletRequest requests, String[] skuId) {
        //在session中获取accessToken
        String accessToken = (String) requests.getAttribute("accessToken");
        if (ObjectUtil.isEmpty(accessToken)) {
            //如果过期了就再生成一个accessToken
            accessToken = getAccessToken(requests);
            //将token存到session中
            HttpSession session = requests.getSession();
            session.setAttribute("accessToken", accessToken);
            //设置过期时间为24小时
            session.setMaxInactiveInterval(60 * 60 * 24);
        }
        JSONObject body = new JSONObject();
        JSONObject requestData = new JSONObject();
        try {
            body.put("appKey", APPKEY);
            body.put("accessToken", accessToken);
            body.put("skuId", skuId);
            requestData.put("data", body);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8"), String.valueOf(requestData));
        OkHttpClient okHttpClient = new OkHttpClient();
        //创建一个请求对象
        Request request = new Request.Builder()
                .url("http://" + IP + "/ecservice/getProductReview")
                .post(requestBody)
                .build();
        //发送请求获取响应
        Response response = null;
        try {
            response = okHttpClient.newCall(request).execute();
            //判断请求是否成功
            if (response.isSuccessful()) {
                //判空
                if (response.body() != null) {
                    //打印服务端返回结果
                    log.info(response.body().string());
                    JSONObject jsonObject = JSONUtil.parseObj(response.body().string());
                    String isSuccess = (String) jsonObject.get("isSuccess");
                    if ("true".equals(isSuccess)) {
                        return jsonObject;
                    }
                }
            } else {
                return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取商品图片接口
     *
     * @return
     */
    public synchronized static JSONObject getProductImage(HttpServletRequest requests, String[] sku) {
        //在session中获取accessToken
        String accessToken = (String) requests.getAttribute("accessToken");
        if (ObjectUtil.isEmpty(accessToken)) {
            //如果过期了就再生成一个accessToken
            accessToken = getAccessToken(requests);
            //将token存到session中
            HttpSession session = requests.getSession();
            session.setAttribute("accessToken", accessToken);
            //设置过期时间为24小时
            session.setMaxInactiveInterval(60 * 60 * 24);
        }
        JSONObject body = new JSONObject();
        JSONObject requestData = new JSONObject();
        try {
            body.put("appKey", APPKEY);
            body.put("accessToken", accessToken);
            body.put("sku", sku);
            requestData.put("data", body);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8"), String.valueOf(requestData));
        OkHttpClient okHttpClient = new OkHttpClient();
        //创建一个请求对象
        Request request = new Request.Builder()
                .url("http://" + IP + "/ecservice/getProductImage")
                .post(requestBody)
                .build();
        //发送请求获取响应
        Response response = null;
        try {
            response = okHttpClient.newCall(request).execute();
            //判断请求是否成功
            if (response.isSuccessful()) {
                //判空
                if (response.body() != null) {
                    //打印服务端返回结果
                    log.info(response.body().string());
                    JSONObject jsonObject = JSONUtil.parseObj(response.body().string());
                    String isSuccess = (String) jsonObject.get("isSuccess");
                    if ("true".equals(isSuccess)) {
                        JSONObject result = JSONUtil.parseObj(String.valueOf(jsonObject.get("result")));
                        return result;
                    }
                }
            } else {
                return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 商品上下架状态查询接口
     *
     * @return
     */
    public synchronized static JSONObject getProductOnShelvesInfo(HttpServletRequest requests, String[] sku) {
        //在session中获取accessToken
        String accessToken = (String) requests.getAttribute("accessToken");
        if (ObjectUtil.isEmpty(accessToken)) {
            //如果过期了就再生成一个accessToken
            accessToken = getAccessToken(requests);
            //将token存到session中
            HttpSession session = requests.getSession();
            session.setAttribute("accessToken", accessToken);
            //设置过期时间为24小时
            session.setMaxInactiveInterval(60 * 60 * 24);
        }
        JSONObject body = new JSONObject();
        JSONObject requestData = new JSONObject();
        try {
            body.put("appKey", APPKEY);
            body.put("accessToken", accessToken);
            body.put("sku", sku);
            requestData.put("data", body);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8"), String.valueOf(requestData));
        OkHttpClient okHttpClient = new OkHttpClient();
        //创建一个请求对象
        Request request = new Request.Builder()
                .url("http://" + IP + "/ecservice/getProductOnShelvesInfo")
                .post(requestBody)
                .build();
        //发送请求获取响应
        Response response = null;
        try {
            response = okHttpClient.newCall(request).execute();
            //判断请求是否成功
            if (response.isSuccessful()) {
                //判空
                if (response.body() != null) {
                    //打印服务端返回结果
                    log.info(response.body().string());
                    JSONObject jsonObject = JSONUtil.parseObj(response.body().string());
                    String isSuccess = (String) jsonObject.get("isSuccess");
                    if ("true".equals(isSuccess)) {
                        JSONObject onShelvesList = JSONUtil.parseObj(String.valueOf(jsonObject.get("onShelvesList")));
                        return onShelvesList;
                    }
                }
            } else {
                return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 查询省信息接口
     *
     * @return
     */
    public synchronized static JSONObject getProvinceInfo(HttpServletRequest requests) {
        //在session中获取accessToken
        String accessToken = (String) requests.getAttribute("accessToken");
        if (ObjectUtil.isEmpty(accessToken)) {
            //如果过期了就再生成一个accessToken
            accessToken = getAccessToken(requests);
            //将token存到session中
            HttpSession session = requests.getSession();
            session.setAttribute("accessToken", accessToken);
            //设置过期时间为24小时
            session.setMaxInactiveInterval(60 * 60 * 24);
        }
        JSONObject body = new JSONObject();
        JSONObject requestData = new JSONObject();
        try {
            body.put("appKey", APPKEY);
            body.put("accessToken", accessToken);
            requestData.put("data", body);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8"), String.valueOf(requestData));
        OkHttpClient okHttpClient = new OkHttpClient();
        //创建一个请求对象
        Request request = new Request.Builder()
                .url("http://" + IP + "/ecservice/getProvinceInfo")
                .post(requestBody)
                .build();
        //发送请求获取响应
        Response response = null;
        try {
            response = okHttpClient.newCall(request).execute();
            //判断请求是否成功
            if (response.isSuccessful()) {
                //判空
                if (response.body() != null) {
                    //打印服务端返回结果
                    log.info(response.body().string());
                    JSONObject jsonObject = JSONUtil.parseObj(response.body().string());
                    String isSuccess = (String) jsonObject.get("isSuccess");
                    if ("true".equals(isSuccess)) {
                        JSONObject province = JSONUtil.parseObj(String.valueOf(jsonObject.get("province")));
                        return province;
                    }
                }
            } else {
                return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 查询市信息接口
     *
     * @return
     */
    public synchronized static JSONObject getCityInfo(HttpServletRequest requests, String provinceId) {
        //在session中获取accessToken
        String accessToken = (String) requests.getAttribute("accessToken");
        if (ObjectUtil.isEmpty(accessToken)) {
            //如果过期了就再生成一个accessToken
            accessToken = getAccessToken(requests);
            //将token存到session中
            HttpSession session = requests.getSession();
            session.setAttribute("accessToken", accessToken);
            //设置过期时间为24小时
            session.setMaxInactiveInterval(60 * 60 * 24);
        }
        JSONObject body = new JSONObject();
        JSONObject requestData = new JSONObject();
        try {
            body.put("appKey", APPKEY);
            body.put("accessToken", accessToken);
            body.put("provinceId", provinceId);
            requestData.put("data", body);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8"), String.valueOf(requestData));
        OkHttpClient okHttpClient = new OkHttpClient();
        //创建一个请求对象
        Request request = new Request.Builder()
                .url("http://" + IP + "/ecservice/getCityInfo")
                .post(requestBody)
                .build();
        //发送请求获取响应
        Response response = null;
        try {
            response = okHttpClient.newCall(request).execute();
            //判断请求是否成功
            if (response.isSuccessful()) {
                //判空
                if (response.body() != null) {
                    //打印服务端返回结果
                    log.info(response.body().string());
                    JSONObject jsonObject = JSONUtil.parseObj(response.body().string());
                    String isSuccess = (String) jsonObject.get("isSuccess");
                    if ("true".equals(isSuccess)) {
                        JSONObject city = JSONUtil.parseObj(String.valueOf(jsonObject.get("city")));
                        return city;
                    }
                }
            } else {
                return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 查询区信息接口
     *
     * @return
     */
    public synchronized static JSONObject getDistrictInfo(HttpServletRequest requests, String cityId) {
        //在session中获取accessToken
        String accessToken = (String) requests.getAttribute("accessToken");
        if (ObjectUtil.isEmpty(accessToken)) {
            //如果过期了就再生成一个accessToken
            accessToken = getAccessToken(requests);
            //将token存到session中
            HttpSession session = requests.getSession();
            session.setAttribute("accessToken", accessToken);
            //设置过期时间为24小时
            session.setMaxInactiveInterval(60 * 60 * 24);
        }
        JSONObject body = new JSONObject();
        JSONObject requestData = new JSONObject();
        try {
            body.put("appKey", APPKEY);
            body.put("accessToken", accessToken);
            body.put("cityId", cityId);
            requestData.put("data", body);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8"), String.valueOf(requestData));
        OkHttpClient okHttpClient = new OkHttpClient();
        //创建一个请求对象
        Request request = new Request.Builder()
                .url("http://" + IP + "/ecservice/getDistrictInfo")
                .post(requestBody)
                .build();
        //发送请求获取响应
        Response response = null;
        try {
            response = okHttpClient.newCall(request).execute();
            //判断请求是否成功
            if (response.isSuccessful()) {
                //判空
                if (response.body() != null) {
                    //打印服务端返回结果
                    log.info(response.body().string());
                    JSONObject jsonObject = JSONUtil.parseObj(response.body().string());
                    String isSuccess = (String) jsonObject.get("isSuccess");
                    if ("true".equals(isSuccess)) {
                        JSONObject district = JSONUtil.parseObj(String.valueOf(jsonObject.get("district")));
                        return district;
                    }
                }
            } else {
                return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 查询镇信息接口
     *
     * @return
     */
    public synchronized static JSONObject getTownInfo(HttpServletRequest requests, String cityId, String countyId) {
        //在session中获取accessToken
        String accessToken = (String) requests.getAttribute("accessToken");
        if (ObjectUtil.isEmpty(accessToken)) {
            //如果过期了就再生成一个accessToken
            accessToken = getAccessToken(requests);
            //将token存到session中
            HttpSession session = requests.getSession();
            session.setAttribute("accessToken", accessToken);
            //设置过期时间为24小时
            session.setMaxInactiveInterval(60 * 60 * 24);
        }
        JSONObject body = new JSONObject();
        JSONObject requestData = new JSONObject();
        try {
            body.put("appKey", APPKEY);
            body.put("accessToken", accessToken);
            body.put("cityId", cityId);
            body.put("countyId", countyId);
            requestData.put("data", body);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8"), String.valueOf(requestData));
        OkHttpClient okHttpClient = new OkHttpClient();
        //创建一个请求对象
        Request request = new Request.Builder()
                .url("http://" + IP + "/ecservice/getTownInfo")
                .post(requestBody)
                .build();
        //发送请求获取响应
        Response response = null;
        try {
            response = okHttpClient.newCall(request).execute();
            //判断请求是否成功
            if (response.isSuccessful()) {
                //判空
                if (response.body() != null) {
                    //打印服务端返回结果
                    log.info(response.body().string());
                    JSONObject jsonObject = JSONUtil.parseObj(response.body().string());
                    String isSuccess = (String) jsonObject.get("isSuccess");
                    if ("true".equals(isSuccess)) {
                        JSONObject town = JSONUtil.parseObj(String.valueOf(jsonObject.get("town")));
                        return town;
                    }
                }
            } else {
                return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 商品库存查询接口
     *
     * @return
     */
    public synchronized static JSONObject getProductInventory(HttpServletRequest requests, String sku, Integer num, String cityId, String countyId) {
        //在session中获取accessToken
        String accessToken = (String) requests.getAttribute("accessToken");
        if (ObjectUtil.isEmpty(accessToken)) {
            //如果过期了就再生成一个accessToken
            accessToken = getAccessToken(requests);
            //将token存到session中
            HttpSession session = requests.getSession();
            session.setAttribute("accessToken", accessToken);
            //设置过期时间为24小时
            session.setMaxInactiveInterval(60 * 60 * 24);
        }
        JSONObject body = new JSONObject();
        JSONObject requestData = new JSONObject();
        try {
            body.put("appKey", APPKEY);
            body.put("accessToken", accessToken);
            body.put("sku", sku);
            body.put("num", num);
            body.put("cityId", cityId);
            body.put("countyId", countyId);
            requestData.put("data", body);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8"), String.valueOf(requestData));
        OkHttpClient okHttpClient = new OkHttpClient();
        //创建一个请求对象
        Request request = new Request.Builder()
                .url("http://" + IP + "/ecservice/getProductInventory")
                .post(requestBody)
                .build();
        //发送请求获取响应
        Response response = null;
        try {
            response = okHttpClient.newCall(request).execute();
            //判断请求是否成功
            if (response.isSuccessful()) {
                //判空
                if (response.body() != null) {
                    //打印服务端返回结果
                    log.info(response.body().string());
                    JSONObject jsonObject = JSONUtil.parseObj(response.body().string());
                    String isSuccess = (String) jsonObject.get("isSuccess");
                    if ("true".equals(isSuccess)) {
                        return jsonObject;
                    }
                }
            } else {
                return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 商品库存状态批量查询接口
     *
     * @return
     */
    public synchronized static JSONObject bathQueryInventoryStatus(HttpServletRequest requests, String[] skus, String cityId) {
        //在session中获取accessToken
        String accessToken = (String) requests.getAttribute("accessToken");
        if (ObjectUtil.isEmpty(accessToken)) {
            //如果过期了就再生成一个accessToken
            accessToken = getAccessToken(requests);
            //将token存到session中
            HttpSession session = requests.getSession();
            session.setAttribute("accessToken", accessToken);
            //设置过期时间为24小时
            session.setMaxInactiveInterval(60 * 60 * 24);
        }
        JSONObject body = new JSONObject();
        JSONObject requestData = new JSONObject();
        try {
            body.put("appKey", APPKEY);
            body.put("accessToken", accessToken);
            body.put("skus", skus);
            body.put("cityId", cityId);
            requestData.put("data", body);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8"), String.valueOf(requestData));
        OkHttpClient okHttpClient = new OkHttpClient();
        //创建一个请求对象
        Request request = new Request.Builder()
                .url("http://" + IP + "/ecservice/bathQueryInventoryStatus")
                .post(requestBody)
                .build();
        //发送请求获取响应
        Response response = null;
        try {
            response = okHttpClient.newCall(request).execute();
            //判断请求是否成功
            if (response.isSuccessful()) {
                //判空
                if (response.body() != null) {
                    //打印服务端返回结果
                    log.info(response.body().string());
                    JSONObject jsonObject = JSONUtil.parseObj(response.body().string());
                    String isSuccess = (String) jsonObject.get("isSuccess");
                    if ("true".equals(isSuccess)) {
                        JSONObject result = JSONUtil.parseObj(String.valueOf(jsonObject.get("result")));
                        return result;
                    }
                }
            } else {
                return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取商品折扣价格接口
     *
     * @return
     */
    public synchronized static JSONObject queryCountPrice(HttpServletRequest requests, String[] sku, String cityId) {
        //在session中获取accessToken
        String accessToken = (String) requests.getAttribute("accessToken");
        if (ObjectUtil.isEmpty(accessToken)) {
            //如果过期了就再生成一个accessToken
            accessToken = getAccessToken(requests);
            //将token存到session中
            HttpSession session = requests.getSession();
            session.setAttribute("accessToken", accessToken);
            //设置过期时间为24小时
            session.setMaxInactiveInterval(60 * 60 * 24);
        }
        JSONObject body = new JSONObject();
        JSONObject requestData = new JSONObject();
        try {
            body.put("appKey", APPKEY);
            body.put("accessToken", accessToken);
            body.put("sku", sku);
            body.put("cityId", cityId);
            requestData.put("data", body);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8"), String.valueOf(requestData));
        OkHttpClient okHttpClient = new OkHttpClient();
        //创建一个请求对象
        Request request = new Request.Builder()
                .url("http://" + IP + "/ecservice/queryCountPrice")
                .post(requestBody)
                .build();
        //发送请求获取响应
        Response response = null;
        try {
            response = okHttpClient.newCall(request).execute();
            //判断请求是否成功
            if (response.isSuccessful()) {
                //判空
                if (response.body() != null) {
                    //打印服务端返回结果
                    log.info(response.body().string());
                    JSONObject jsonObject = JSONUtil.parseObj(response.body().string());
                    String isSuccess = (String) jsonObject.get("isSuccess");
                    if ("true".equals(isSuccess)) {
                        JSONObject priceList = JSONUtil.parseObj(String.valueOf(jsonObject.get("priceList")));
                        return priceList;
                    }
                }
            } else {
                return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 创建订单接口
     *
     * @return
     */
    public synchronized static JSONObject createOrder(HttpServletRequest requests, String tradeNo, SkuArray[] sku, String name, String danweiguid,
                                                      String provinceId, String cityId, String countyId, String townId, String address, String phone,
                                                      String mobile, String email, String remark, Integer invoiceState, Integer invoiceType, String companyName,
                                                      String invoiceContent, String invoiceTaxNo, BigDecimal amount, BigDecimal freight, String payment) {
        //在session中获取accessToken
        String accessToken = (String) requests.getAttribute("accessToken");
        if (ObjectUtil.isEmpty(accessToken)) {
            //如果过期了就再生成一个accessToken
            accessToken = getAccessToken(requests);
            //将token存到session中
            HttpSession session = requests.getSession();
            session.setAttribute("accessToken", accessToken);
            //设置过期时间为24小时
            session.setMaxInactiveInterval(60 * 60 * 24);
        }
        JSONObject body = new JSONObject();
        JSONObject requestData = new JSONObject();
        try {
            body.put("appKey", APPKEY);
            body.put("accessToken", accessToken);
            body.put("tradeNo", tradeNo);
            body.put("sku", sku);
            body.put("name", name);
            body.put("danweiguid", danweiguid);
            body.put("provinceId", provinceId);
            body.put("cityId", cityId);
            body.put("countyId", countyId);
            body.put("townId", townId);
            body.put("address", address);
            body.put("phone", phone);
            body.put("mobile", mobile);
            body.put("email", email);
            body.put("remark", remark);
            body.put("invoiceState", invoiceState);
            body.put("invoiceType", invoiceType);
            body.put("companyName", companyName);
            body.put("invoiceContent", invoiceContent);
            body.put("invoiceTaxNo", invoiceTaxNo);
            body.put("amount", amount);
            body.put("freight", freight);
            body.put("payment", payment);
            requestData.put("data", body);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8"), String.valueOf(requestData));
        OkHttpClient okHttpClient = new OkHttpClient();
        //创建一个请求对象
        Request request = new Request.Builder()
                .url("http://" + IP + "/ecservice/createOrder")
                .post(requestBody)
                .build();
        //发送请求获取响应
        Response response = null;
        try {
            response = okHttpClient.newCall(request).execute();
            //判断请求是否成功
            if (response.isSuccessful()) {
                //判空
                if (response.body() != null) {
                    //打印服务端返回结果
                    log.info(response.body().string());
                    JSONObject jsonObject = JSONUtil.parseObj(response.body().string());
                    String isSuccess = (String) jsonObject.get("isSuccess");
                    if ("true".equals(isSuccess)) {
                        return jsonObject;
                    }
                }
            } else {
                return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 订单退货申请接口
     *
     * @return
     */
    public synchronized static JSONObject backGoodsApply(HttpServletRequest requests, String tradeNO, SkuSalesReturn[] sku,Integer backType,String getAddress) {
        //在session中获取accessToken
        String accessToken = (String) requests.getAttribute("accessToken");
        if (ObjectUtil.isEmpty(accessToken)) {
            //如果过期了就再生成一个accessToken
            accessToken = getAccessToken(requests);
            //将token存到session中
            HttpSession session = requests.getSession();
            session.setAttribute("accessToken", accessToken);
            //设置过期时间为24小时
            session.setMaxInactiveInterval(60 * 60 * 24);
        }
        JSONObject body = new JSONObject();
        JSONObject requestData = new JSONObject();
        try {
            body.put("appKey", APPKEY);
            body.put("accessToken", accessToken);
            body.put("tradeNO", tradeNO);
            body.put("sku", sku);
            body.put("backType", backType);
            body.put("getAddress", getAddress);
            requestData.put("data", body);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8"), String.valueOf(requestData));
        OkHttpClient okHttpClient = new OkHttpClient();
        //创建一个请求对象
        Request request = new Request.Builder()
                .url("http://" + IP + "/ecservice/backGoodsApply")
                .post(requestBody)
                .build();
        //发送请求获取响应
        Response response = null;
        try {
            response = okHttpClient.newCall(request).execute();
            //判断请求是否成功
            if (response.isSuccessful()) {
                //判空
                if (response.body() != null) {
                    //打印服务端返回结果
                    log.info(response.body().string());
                    JSONObject jsonObject = JSONUtil.parseObj(response.body().string());
                    String isSuccess = (String) jsonObject.get("isSuccess");
                    if ("true".equals(isSuccess)) {
                        return jsonObject;
                    }
                }
            } else {
                return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 订单退货通知接口
     *
     * @return
     */
    public synchronized static JSONObject backGoodsApplyReturn(HttpServletRequest requests, String isApplyPass, String failReason,String tradeNo,String backNo,
                                                               String backAddress,String linkName,String linkTel) {
        //在session中获取accessToken
        String accessToken = (String) requests.getAttribute("accessToken");
        if (ObjectUtil.isEmpty(accessToken)) {
            //如果过期了就再生成一个accessToken
            accessToken = getAccessToken(requests);
            //将token存到session中
            HttpSession session = requests.getSession();
            session.setAttribute("accessToken", accessToken);
            //设置过期时间为24小时
            session.setMaxInactiveInterval(60 * 60 * 24);
        }
        JSONObject body = new JSONObject();
        JSONObject requestData = new JSONObject();
        try {
            body.put("ecName", ECNAME);
            body.put("platformCode", PLATFORMCODE);
            body.put("isApplyPass", isApplyPass);
            body.put("failReason", failReason);
            body.put("tradeNo", tradeNo);
            body.put("backNo", backNo);
            body.put("backAddress", backAddress);
            body.put("linkName", linkName);
            body.put("linkTel", linkTel);
            requestData.put("data", body);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8"), String.valueOf(requestData));
        OkHttpClient okHttpClient = new OkHttpClient();
        //创建一个请求对象
        Request request = new Request.Builder()
                .url("http://" + IP + "/rest/info/backGoodsApplyReturn")
                .post(requestBody)
                .build();
        //发送请求获取响应
        Response response = null;
        try {
            response = okHttpClient.newCall(request).execute();
            //判断请求是否成功
            if (response.isSuccessful()) {
                //判空
                if (response.body() != null) {
                    //打印服务端返回结果
                    log.info(response.body().string());
                    JSONObject jsonObject = JSONUtil.parseObj(response.body().string());
                    String isSuccess = (String) jsonObject.get("isSuccess");
                    if ("true".equals(isSuccess)) {
                        return jsonObject;
                    }
                }
            } else {
                return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取订单物流信息接口
     *
     * @return
     */
    public synchronized static JSONObject getOrderLogistics(HttpServletRequest requests, String orderId, String skuId) {
        //在session中获取accessToken
        String accessToken = (String) requests.getAttribute("accessToken");
        if (ObjectUtil.isEmpty(accessToken)) {
            //如果过期了就再生成一个accessToken
            accessToken = getAccessToken(requests);
            //将token存到session中
            HttpSession session = requests.getSession();
            session.setAttribute("accessToken", accessToken);
            //设置过期时间为24小时
            session.setMaxInactiveInterval(60 * 60 * 24);
        }
        JSONObject body = new JSONObject();
        JSONObject requestData = new JSONObject();
        try {
            body.put("appKey", APPKEY);
            body.put("accessToken", accessToken);
            body.put("orderId", orderId);
            body.put("skuId", skuId);
            requestData.put("data", body);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8"), String.valueOf(requestData));
        OkHttpClient okHttpClient = new OkHttpClient();
        //创建一个请求对象
        Request request = new Request.Builder()
                .url("http://" + IP + "/ecservice/getOrderLogistics")
                .post(requestBody)
                .build();
        //发送请求获取响应
        Response response = null;
        try {
            response = okHttpClient.newCall(request).execute();
            //判断请求是否成功
            if (response.isSuccessful()) {
                //判空
                if (response.body() != null) {
                    //打印服务端返回结果
                    log.info(response.body().string());
                    JSONObject jsonObject = JSONUtil.parseObj(response.body().string());
                    String isSuccess = (String) jsonObject.get("isSuccess");
                    if ("true".equals(isSuccess)) {
                        return jsonObject;
                    }
                }
            } else {
                return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取订单状态接口
     *
     * @return
     */
    public synchronized static JSONObject getOrderStatus(HttpServletRequest requests, String orderId) {
        //在session中获取accessToken
        String accessToken = (String) requests.getAttribute("accessToken");
        if (ObjectUtil.isEmpty(accessToken)) {
            //如果过期了就再生成一个accessToken
            accessToken = getAccessToken(requests);
            //将token存到session中
            HttpSession session = requests.getSession();
            session.setAttribute("accessToken", accessToken);
            //设置过期时间为24小时
            session.setMaxInactiveInterval(60 * 60 * 24);
        }
        JSONObject body = new JSONObject();
        JSONObject requestData = new JSONObject();
        try {
            body.put("appKey", APPKEY);
            body.put("accessToken", accessToken);
            body.put("orderId", orderId);
            requestData.put("data", body);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8"), String.valueOf(requestData));
        OkHttpClient okHttpClient = new OkHttpClient();
        //创建一个请求对象
        Request request = new Request.Builder()
                .url("http://" + IP + "/ecservice/getOrderStatus")
                .post(requestBody)
                .build();
        //发送请求获取响应
        Response response = null;
        try {
            response = okHttpClient.newCall(request).execute();
            //判断请求是否成功
            if (response.isSuccessful()) {
                //判空
                if (response.body() != null) {
                    //打印服务端返回结果
                    log.info(response.body().string());
                    JSONObject jsonObject = JSONUtil.parseObj(response.body().string());
                    String isSuccess = (String) jsonObject.get("isSuccess");
                    if ("true".equals(isSuccess)) {
                        return jsonObject;
                    }
                }
            } else {
                return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 发票查询
     *
     * @return
     */
    public synchronized static JSONObject getInvoiceInfo(HttpServletRequest requests, String tradeNo) {
        //在session中获取accessToken
        String accessToken = (String) requests.getAttribute("accessToken");
        if (ObjectUtil.isEmpty(accessToken)) {
            //如果过期了就再生成一个accessToken
            accessToken = getAccessToken(requests);
            //将token存到session中
            HttpSession session = requests.getSession();
            session.setAttribute("accessToken", accessToken);
            //设置过期时间为24小时
            session.setMaxInactiveInterval(60 * 60 * 24);
        }
        JSONObject body = new JSONObject();
        JSONObject requestData = new JSONObject();
        try {
            body.put("appKey", APPKEY);
            body.put("accessToken", accessToken);
            body.put("tradeNo", tradeNo);
            requestData.put("data", body);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8"), String.valueOf(requestData));
        OkHttpClient okHttpClient = new OkHttpClient();
        //创建一个请求对象
        Request request = new Request.Builder()
                .url("http://" + IP + "/ecservice/getInvoiceInfo")
                .post(requestBody)
                .build();
        //发送请求获取响应
        Response response = null;
        try {
            response = okHttpClient.newCall(request).execute();
            //判断请求是否成功
            if (response.isSuccessful()) {
                //判空
                if (response.body() != null) {
                    //打印服务端返回结果
                    log.info(response.body().string());
                    JSONObject jsonObject = JSONUtil.parseObj(response.body().string());
                    String isSuccess = (String) jsonObject.get("isSuccess");
                    if ("true".equals(isSuccess)) {
                        return jsonObject;
                    }
                }
            } else {
                return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
